package com.eveinfo.display.asset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.api.asset.ApiAsset;
import com.eveinfo.sql.data.Item;
import com.eveinfo.sql.data.Station;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.Tools;

public class AssetsByStationAdapter extends BaseAdapter {

	private Context context;
	private final ArrayList<Station> allStation;
	private final ArrayList<Station> currentStations;
	private Integer[] unfold;
	private AssetsSearchFilter filter;
	private int count;

	public AssetsByStationAdapter(Context context, ListView listView, ArrayList<Station> stations) {
		this.context = context;
		this.allStation = new ArrayList<Station>(stations);
		this.currentStations = new ArrayList<Station>(stations);
		setSort(new GroupComparator<ApiAsset>());
		final int size = stations.size();
		this.unfold = new Integer[size];
		for (int i = 0; i < size; i++) {
			unfold[i] = 0;
		}
		listView.setAdapter(this);
		this.filter = new AssetsSearchFilter();
		calcCount();
	}

	@Override
	public int getCount() {
		return count;
	}

	private void calcCount() {
		int total = 0;
		final ArrayList<Station> stations = this.currentStations;
		int size = stations.size();
		for (int i = 0; i < size; i++) {
			total++;
			if (unfold[i] == 1) {
				total += stations.get(i).getAssets().size();
			}
		}
		count = total;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int pos = position;
		final ArrayList<Station> stations = this.currentStations;
		int stationsSize = stations.size();
		for (int i = 0; i < stationsSize; i++) {
			Station station = stations.get(i);
			final int y = i;
			int size = 1;
			final int assetSize = station.getAssets().size();
			if (unfold[i] == 1) {
				size += assetSize;
			}
			if (pos == 0) {
				LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View view = vi.inflate(R.layout.itemr, null);
				
				
				view.setPadding(5, 3, 0, 3);
				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (unfold[y] == 0) {
							unfold[y] = 1;
						} else {
							unfold[y] = 0;
						}
						calcCount();
						notifyDataSetChanged();
					}
				});

				String stationName = station.getStationName();
				if (stationName == null) {
					stationName = "unKnown";
				}
				String[] split = stationName.split(" - ");
				String stationName1 = stationName;
				String stationName2 = "";
				if (split.length == 3) {
					stationName1 = split[0] + " - " + split[1];
					stationName2 = split[2];
				}
				
				TextView stationTV = (TextView) view.findViewById(R.id.TVItemRStation);
				stationTV.setText(stationName1);
				TextView nameTV = (TextView) view.findViewById(R.id.TVItemRName);
				nameTV.setText(stationName2);
				TextView quantityTV = (TextView) view.findViewById(R.id.TVItemRNumber);
				quantityTV.setText(""+assetSize);
				
				return view;
			} else if (pos < size) {
				LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View view = vi.inflate(R.layout.itemr2, null);
				final ApiAsset asset = station.get(pos - 1);
				final Item item = asset.getItem();

				ImageView imageIV = (ImageView) view.findViewById(R.id.IVItemR2Picture);
				imageIV.setImageDrawable(Tools.getItemPicture(item, EveInfo.getPreferences().getAssetsImageSize()));

				TextView nameTV = (TextView) view.findViewById(R.id.TVItemR2Name);
				nameTV.setText(item.getTypeName());
				nameTV.setTextColor(Color.WHITE);

				TextView numberTV = (TextView) view.findViewById(R.id.TVItemR2Number);
				numberTV.setText("" + asset.getQuantity());
				numberTV.setTextColor(Color.WHITE);

				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						ActivityLauncher.displayItem(context, asset.getTypeID());
					}
				});
				view.setOnLongClickListener(new OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						ActivityLauncher.displayItemInfo(context, asset.getTypeID());
						return true;
					}
				});
				return view;
			}
			pos -= size;
		}
		return null;
	}

	public void setSeach(String text) {
		System.out.println("setSeach");
		currentStations.clear();
		if (text.equals("")) {
			currentStations.addAll(allStation);
		} else {
			final ArrayList<Station> stations = this.allStation;
			int size = stations.size();
			for (int i = 0; i < size; i++) {
				final Station station = stations.get(i);
				ArrayList<ApiAsset> assets = station.getAssets();
				ArrayList<ApiAsset> currentAssets = new ArrayList<ApiAsset>();
				int assetSize = assets.size();
				for (int j = 0; j < assetSize; j++) {
					ApiAsset asset = assets.get(j);
					if (filter.isDisplay(asset, text)) {
						currentAssets.add(asset);
					}
				}
				if (!currentAssets.isEmpty()) {
					final Station station2 = new Station(station.getStationName());
					station2.addAsset(currentAssets);
					currentStations.add(station2);
				}
			}
		}
		final int size2 = currentStations.size();
		this.unfold = new Integer[size2];
		for (int i = 0; i < size2; i++) {
			unfold[i] = 0;
		}
		calcCount();
		notifyDataSetChanged();
	}

	public class AssetsSearchFilter {
		public boolean isDisplay(ApiAsset asset, String search) {
			if (search.equals(""))
				return true;
			String typeName = asset.getItem().getTypeName();
			int length = search.length();
			if (typeName.regionMatches(true, 0, search, 0, length)) {
				return true;
			}
			String[] split = typeName.split(" ");
			for (String s : split) {
				if (s.regionMatches(true, 0, search, 0, length)) {
					return true;
				}
			}
			return false;
		}
	}

	public void setSort(Comparator<ApiAsset> sorter) {
		ArrayList<Station> stations = this.currentStations;
		int size = stations.size();
		for (int i = 0; i < size; i++) {
			ArrayList<ApiAsset> assets = stations.get(i).getAssets();
			Collections.sort(assets, sorter);
		}

		stations = this.allStation;
		size = stations.size();
		for (int i = 0; i < size; i++) {
			ArrayList<ApiAsset> assets = stations.get(i).getAssets();
			Collections.sort(assets, sorter);
		}
		notifyDataSetChanged();
	}
}
